﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace Autofocus
{
    public partial class Form1 : Form
    {
        #region Camera UI Controls

        private void UpdateControls(MotiCam camera)
        {
            comboBox_Binning.Items.Clear();
            for (int i = 0; i < camera.binCount; i++)
            {
                String BinString = camera.m_pWidth[i].ToString() + " x " + camera.m_pHeight[i].ToString();
                comboBox_Binning.Items.Add(BinString);
                comboBox_Binning.SelectedIndex = 0;
            }

            numericUpDown_Exposure.Minimum = Convert.ToDecimal(camera.m_fMinExposure);
            numericUpDown_Exposure.Maximum = Convert.ToDecimal(camera.m_fMaxExposure);
            numericUpDown_Exposure.Value = Convert.ToDecimal(camera.m_fExposure);

            trackBar_Offset.Minimum = camera.m_iMinOffset;
            trackBar_Offset.Maximum = camera.m_iMaxOffset;
            trackBar_Offset.Value = camera.m_iOffset;
            textBox_Offset.Text = camera.m_iOffset.ToString();

            trackBar_Red_Gain.Minimum = 0;
            trackBar_Red_Gain.Maximum = camera.m_iGainCount - 1;
            trackBar_Red_Gain.Value = camera.m_RedGainIndex;
            textBox_Red_Gain.Text = camera.m_pGainList[camera.m_RedGainIndex].ToString();

            trackBar_Green_Gain.Minimum = 0;
            trackBar_Green_Gain.Maximum = camera.m_iGainCount - 1;
            trackBar_Green_Gain.Value = camera.m_GreenGainIndex;
            textBox_Green_Gain.Text = camera.m_pGainList[camera.m_GreenGainIndex].ToString();

            trackBar_Blue_Gain.Minimum = 0;
            trackBar_Blue_Gain.Maximum = camera.m_iGainCount - 1;
            trackBar_Blue_Gain.Value = camera.m_BlueGainIndex;
            textBox_Blue_Gain.Text = camera.m_pGainList[camera.m_BlueGainIndex].ToString();
        }

        private void comboBox_Cameras_SelectedIndexChanged(object sender, EventArgs e)
        {
            IntPtr hCameraHandle;
            hCameraHandle = hCameraList[comboBox_Cameras.SelectedIndex];
            if (My_MotiCam != null)
            {
                My_MotiCam.CloseCamera();
            }
            My_MotiCam = new MotiCam(hCameraHandle);
            if (My_MotiCam.OpenCamera())
            {
                UpdateControls(My_MotiCam);

            }
            else
                MessageBox.Show("Unable to open camera");

        }

        private void trackBar_Offset_ValueChanged(object sender, EventArgs e)
        {
            My_MotiCam.m_iOffset = trackBar_Offset.Value;
            My_MotiCam.setRGBOffset();
            UpdateControls(My_MotiCam);
        }

        private void trackBar_Red_Gain_ValueChanged(object sender, EventArgs e)
        {
            My_MotiCam.m_RedGainIndex = trackBar_Red_Gain.Value;
            My_MotiCam.setRGBGainIndex();
            UpdateControls(My_MotiCam);
        }

        private void trackBar_Green_Gain_ValueChanged(object sender, EventArgs e)
        {
            My_MotiCam.m_GreenGainIndex = trackBar_Green_Gain.Value;
            My_MotiCam.setRGBGainIndex();
            UpdateControls(My_MotiCam);
        }

        private void trackBar_Blue_Gain_ValueChanged(object sender, EventArgs e)
        {
            My_MotiCam.m_BlueGainIndex = trackBar_Blue_Gain.Value;
            My_MotiCam.setRGBGainIndex();
            UpdateControls(My_MotiCam);
        }

        private void numericUpDown_Exposure_ValueChanged(object sender, EventArgs e)
        {
            My_MotiCam.m_fExposure = Convert.ToSingle(numericUpDown_Exposure.Value);
            My_MotiCam.setExposure();
        }

        private void btn_Get_Single_Frame_Click(object sender, EventArgs e)
        {
            Image<Bgr, Byte> img = GetImage(My_MotiCam, 0);
            imageBox1.Image = img;
            // img.Dispose(); Adding this line means you can't access the data in the imagebox image any more!
        }

        private void btn_Live_Cam_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
                timer1.Enabled = false;
            else
                timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Image<Bgr, Byte> img = GetImage(My_MotiCam, 0);

            imageBox1.Image = img;
        }

        #endregion Camera UI Controls 
    }
}